package eme.android;

import eme.android.R;
import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.Display;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.Window;
import android.view.WindowManager;

public class Main extends Activity {
	
	int activeView = 0;
	final int MAIN_ITEM_ID = 0, SETTINGS_ITEM_ID = 1;
    String user, password;
	Display display;
	boolean bIsPortrait = true;

	MainView mainView;
	SettingsView settingsView;
	
	/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(eme.android.R.layout.login);
        
		//Remove status and title bars in landscape mode.
		Window win = getWindow();
        if(win.getWindowManager().getDefaultDisplay().getHeight() < win.getWindowManager().getDefaultDisplay().getWidth()){
            //No Statusbar.
            win.setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
            //No Titlebar.
            requestWindowFeature(Window.FEATURE_NO_TITLE);
            requestWindowFeature(Window.FEATURE_PROGRESS);  
    }
        
      //Retrieve saved data from (persistent) storage in a preferences file.
		//(file is private to this activity and has default name = activity name)
		//restoreData();
		
		//Get screen orientation.
		display = ((WindowManager) getSystemService(WINDOW_SERVICE)).getDefaultDisplay();
		if(display.getHeight() > display.getWidth()) bIsPortrait = true; else bIsPortrait = false;
		
		//Create my mainView.
		mainView = new MainView(this);

    } 
    
   
    void saveData(){
    	  //Save whatever data you want persistently saved (in the preferences file).
    	//(file is private to this activity and has default name = activity name)
    	SharedPreferences settings = getPreferences(Context.MODE_PRIVATE);
    	
    	SharedPreferences.Editor e = settings.edit();
       //	e.putString("User", user);
       	//e.putString("Password", password);
      // 	e.commit();
       	
       //	Log.d(TAG,"Saved.");
    }
    void restoreData(){
    	//Restore from data saved (in the preferences file).
    	//(file is private to this activity and has default name = activity name)
    	SharedPreferences settings = getPreferences(Context.MODE_PRIVATE);
 
    	//user = settings.getString("User", "Elisabeth");
    	//password = settings.getString("Password", "password");
 
    	//Log.d(TAG,"Restored.");
    }
    //Creates Options Menu items, called first time menu accessed.
    @Override
    public boolean onCreateOptionsMenu(Menu menu){
		super.onCreateOptionsMenu(menu);
		//Log.d(TAG,"Options Menu set.");
    	menu.add(0, SETTINGS_ITEM_ID, 0, "Settings").setIcon(R.drawable.icon);
        menu.add(0, MAIN_ITEM_ID    , 1, "Main").setIcon(R.drawable.icon);
    	return true;
    }
    
    @Override
    public void onRestart() {
    	//Called after the activity has been stopped, just prior to it being started again.
        //Always followed by onStart().
    	super.onRestart();
		//Log.d(TAG,"Restarted.");
    }  
    @Override
    public void onStart() {
    	//Called just before the activity becomes visible to the user.
        //Followed by onResume() if the activity comes to the foreground, or onStop() if it becomes hidden.
    	super.onStart();
		//Log.d(TAG,"Started.");
    }
    @Override
    public void onResume() {
    	//Called just before the activity starts interacting with the user.
        //At this point the activity is at the top of the activity stack, with user input going to it.
        //Always followed by onPause().
    	super.onResume();
 		//Log.d(TAG,"Resumed.");
    	
		if(activeView==MAIN_ITEM_ID) setContentView(R.layout.login);
		if(activeView==SETTINGS_ITEM_ID){
			//setContentView(settingsView);
			setContentView(R.layout.settings);
			//configSettingsView();
		}
    }
    @Override
    public void onPause() {
    	//Called when the system is about to start resuming another activity.
        //This method is typically used to commit unsaved changes to persistent data, stop animations and other things that may be consuming CPU, and so on.
        //It should do whatever it does very quickly, because the next activity will not be resumed until it returns.
        //Followed either by onResume() if the activity returns back to the front, or by onStop() if it becomes invisible to the user.
    	super.onPause();
 		//Log.d(TAG,"Paused.");
    }  
    @Override
    public void onStop() {
    	//Called when the activity is no longer visible to the user.
        //This may happen because it is being destroyed, or because another activity (either an existing one or a new one) has been resumed and is covering it.
        //Followed either by onRestart() if the activity is coming back to interact with the user, or by onDestroy() if this activity is going away.
        super.onStop();
        
        //Save data to (persistent) storage in a preferences file.
		//(file is private to this activity and has default name = activity name)
		//saveData();
        //Log.d(TAG,"Stopped.");
    }   
    @Override
    public void onDestroy() {     
        //Called before the activity is destroyed.
        //This is the final call that the activity will receive.
        //It could be called either because the activity is finishing (someone called finish() on it), or because the system is temporarily destroying this instance of the activity to save space.
        //You can distinguish between these two scenarios with the isFinishing() method.
    	super.onDestroy();
		//Log.d(TAG,"Destroyed.");
		
		//Declare ticker thread as interrupted, it will soon exit (hopefully).
		//(might need to check truly exited by calling tickerThread.isAlive() and if so, wait and kill again)
		//tickerThread.interrupt();
		
	}
    @Override
    public void onSaveInstanceState(Bundle savedInstanceState){
        //Called by system depending on GUI happenings.
        //(use only for GUI state, not for persistent data)
        //(if not present, system will restore GUI state itself, which might not be desirable)
    	super.onSaveInstanceState(savedInstanceState);
    }
    @Override
    public void onRestoreInstanceState(Bundle savedInstanceState){
    	//Called by system depending on GUI happenings.
        //(use only for GUI state, not for persistent data)
        //(if not present, system will restore GUI state itself, which might not be desirable)
      	super.onRestoreInstanceState(savedInstanceState);
    }

  //Menu handler, called when an Options Menu item is accessed.
    @Override
    public boolean onOptionsItemSelected(MenuItem item){
    	activeView = item.getItemId();
    	switch (item.getItemId()){
       	
    	case MAIN_ITEM_ID:
    		//Log.d(TAG,"Start accessed.");
    		setContentView(R.layout.login);
    		break;//return true;
    		
       	case SETTINGS_ITEM_ID:
    		//Log.d(TAG,"Settings accessed.");
    		//setContentView(settingsView);
       		setContentView(R.layout.settings);
    		//configSettingsView();
    		break;//return true;
    		
		}
		return false;
    }
    
     
    @Override
    public boolean onPrepareOptionsMenu(Menu menu) {
    	 //Called right before the menu is shown, every time it is shown (not just first time menu accessed).
        //You can use this method to efficiently enable/disable items or otherwise dynamically modify the contents.
    	return super.onPrepareOptionsMenu(menu);
    }

    
    //Called when restoring a Settings view (no separate code for that view).
    void configSettingsView(){
    	
		/*userText = (EditText) findViewById(R.id.user);
		userText.setText(user);
		passwordText = (EditText) findViewById(R.id.password);
		passwordText.setText(password);
		
		Button cancel = (Button) findViewById(R.id.cancel);
		cancel.setOnClickListener(new View.OnClickListener(){
	        public void onClick(View v) {
	        	activeView = MAIN_ITEM_ID;
	            setContentView(mainView);
	        }
	        
	    });
		
		Button ok = (Button) findViewById(R.id.ok);
		ok.setOnClickListener(new View.OnClickListener(){
	        public void onClick(View v) {
	            user = userText.getText().toString();
	            password = passwordText.getText().toString();
	            saveData();
	            activeView = MAIN_ITEM_ID;
	            setContentView(mainView);
	        }
	    });
	    */
    }
		
 
    //Keyboard handler
    //Called when a key was pressed (and not handled by any of the views inside of the activity). 
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event){
    	if(keyCode==KeyEvent.KEYCODE_BACK){
    		if(activeView!= MAIN_ITEM_ID){
    			setContentView(mainView);
    			activeView = MAIN_ITEM_ID;
    			return true;
    		}else{
    			return super.onKeyDown(keyCode, event);
    		}
    	}
    	return super.onKeyDown(keyCode, event);
    }

   
}